/**
 * 主渠道管理初始化
 */
var ChannelSuper = {
    id: "ChannelSuperTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
ChannelSuper.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: 'ID', field: 'id', visible: true, align: 'center', valign: 'middle'},
            {title: '公司名称', field: 'companyName', visible: true, align: 'center', valign: 'middle'},
            {title: '渠道名称', field: 'channelName', visible: true, align: 'center', valign: 'middle'},
            {title: '子渠道数量', field: 'subCount', visible: true, align: 'center', valign: 'middle'},
            {title: '激活量', field: 'activeCount', visible: true, align: 'center', valign: 'middle'},
            {title: '注册量', field: 'registerCount', visible: true, align: 'center', valign: 'middle'},
            {title: '申请量', field: 'requestCount', visible: true, align: 'center', valign: 'middle'},
            {title: '成交量', field: 'bargainCount', visible: true, align: 'center', valign: 'middle'},
            {title: '成交额', field: 'bargainAmount', visible: true, align: 'center', valign: 'middle'},
            {title: '状态', field: 'status', visible: true, align: 'center', valign: 'middle'},
            {title: '创建时间', field: 'createTime', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
ChannelSuper.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        ChannelSuper.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加主渠道
 */
ChannelSuper.openAddChannelSuper = function () {
    var index = layer.open({
        type: 2,
        title: '添加主渠道',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/channelSuper/channelSuper_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看主渠道详情
 */
ChannelSuper.openChannelSuperDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '主渠道详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/channelSuper/channelSuper_update/' + ChannelSuper.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除主渠道
 */
ChannelSuper.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/channelSuper/delete", function (data) {
            Feng.success("删除成功!");
            ChannelSuper.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("channelSuperId",this.seItem.id);
        ajax.start();
    }
};
ChannelSuper.getDate = function (a, b) {
    var allData = []
    var channel = []
    var channelData = {
        active: [],
        register: [],
        apply: [],
        deal: [],
        bargainAmount: []
    }
    var dom = document.getElementById("main");
    var myChart = echarts.init(dom);
    var labelOption = {
        show: true,
        rotate: 90,
        align: 'top',
        verticalAlign: 'middle',
        position: 'insideBottom',
        distance: 5,
        fontSize: 12,
    }
    var option = {
        color: ['#c23531','#2f4554', '#61a0a8', '#d48265', '#91c7ae','#749f83'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        legend: {
            data: ['激活数', '注册数', '申请数', '成交数', '成交额'],
            selected: {
                '注册数': false,
                '申请数': false,
                '成交数': false,
                '成交额': false
            },
            selectedMode: 'single'
        },
        toolbox: {
            show: true,
            orient: 'horizontal',
            left: 'right',
            top: 'top',
            feature: {
                mark: {show: true},
                magicType: {show: true, type: ['line', 'bar']},
                restore: {show: true},
                saveAsImage: {show: true}
            }
        },
        xAxis: [
            {
                type: 'category',
                show: true,
                data: channel,
                axisLabel: {
                    interval:0,
                    rotate:40
                }
            }
        ],
        dataZoom: [
            {
                show: true,
                type: 'inside',
                filterMode: 'empty',
                start: 0,
                end: 100
            },
            {
                show: true,
                yAxisIndex: 0,
                filterMode: 'empty',
                width: 15,
                height: '80%',
                showDataShadow: false,
                left: '93%'
            }
        ],
        yAxis: [
            {
                type: 'value'
            }
        ],
        series: [
            {
                name: '激活数',
                type: 'bar',
                label: labelOption,
                data: channelData.active
            },
            {
                name: '注册数',
                type: 'bar',
                label: labelOption,
                data: channelData.register
            },
            {
                name: '申请数',
                type: 'bar',
                label: labelOption,
                data: channelData.apply
            },
            {
                name: '成交数',
                type: 'bar',
                label: labelOption,
                data: channelData.deal
            },
            {
                name: '成交额',
                type: 'bar',
                label: labelOption,
                data: channelData.bargainAmount
            }
        ]
    }

    $.ajax({
        type: "post",
        url: "/countChannelSuper/list",
        data: {'beginDate': a,'endDate': b},
        dataType: "json",
        success: function(data){
            for (var i=1; i<data.length; i++) {
                channel.push(data[i].channelName)
                channelData.active.push(data[i].activeCount)
                channelData.register.push(data[i].registerCount)
                channelData.apply.push(data[i].requestCount)
                channelData.deal.push(data[i].bargainCount)
                channelData.bargainAmount.push(data[i].bargainAmount)
            }
            myChart.setOption(option, true);
        },
        error: function(err) {
            console.log(err)
        }
    });
}
/**
 * 查询主渠道列表
 */
ChannelSuper.search = function () {
    var queryData = {};
    if ($("#beginDate").val() || $("#endDate").val()) {
        queryData['beginDate'] = $("#beginDate").val();
        queryData['endDate'] = $("#endDate").val();
        ChannelSuper.getDate($("#beginDate").val(), $("#endDate").val())
    } else {
        ChannelSuper.table.queryParams = {}
        ChannelSuper.getDate()
    }
    ChannelSuper.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = ChannelSuper.initColumn();
    var table = new BSTable(ChannelSuper.id, "/countChannelSuper/list", defaultColunms);
    table.setPaginationType("client");
    ChannelSuper.table = table.init();
    ChannelSuper.getDate()
});

/**
 * 查看子渠道列表管理
 */
ChannelSuper.subList = function () {
    if (this.check()) {
        location.href = Feng.ctxPath + "/countChannel?channelId="+ChannelSuper.seItem.id;
    }
};
